<?php
header("Content-type: text/html; charset=utf-8");

$page = $_GET['page']; // get the requested page 
$limit = $_GET['rows']; // get how many rows we want to have into the grid 
$sidx = $_GET['sidx']; // get index row - i.e. user click to sort 
$sord = $_GET['sord']; // get the direction 
$event = intval($_GET['eve']);

if ($event == -1)
    return;

$admin = true;
require_once('auth_req.inc.php');

$rs = $dbh->query('select count(*) as count from event_items where e_id=' . $event);

if ($rec = $rs->fetchAll(PDO::FETCH_COLUMN, 0))
{
    $count = $rec[0];
}
else
{
    $count = 0;
}

if( $count > 0 ) 
{ 
    $total_pages = ceil($count/$limit); 
} 
else 
{ 
    $total_pages = 0; 
} 

if ($page > $total_pages) 
    $page=$total_pages; 

$start = $limit * $page - $limit; // do not put $limit*($page - 1) 

if ($start < 0) 
    $start = 0;

$sth = $dbh->prepare('select ei.c_id, ei.prog, ei.e_id, nm.location from event_items ei, network_map nm where nm.c_id = ei.c_id and ei.e_id=:eid order by ei.' . $sidx . ' ' . $sord  .' limit :start,:limit');

$sth->bindParam(':eid', intval($event), PDO::PARAM_INT);
$sth->bindParam(':start', intval($start), PDO::PARAM_INT);
$sth->bindParam(':limit', intval($limit), PDO::PARAM_INT);

$rs = $sth->execute();

$responce->page = intval($page); 
$responce->total = intval($total_pages); 
$responce->records = intval($count); 

$alldb = $sth->fetchAll(PDO::FETCH_ASSOC); 

foreach( $alldb as $key => $val)
{
    $responce->rows[$key]['id'] = $val['e_id'].'-'.$val['c_id'];

    switch ($val['prog'])
    {
        case 9:
            $prog = 'ЖМ';
        break;

        case 10:
            $prog = 'ОС';
        break;
        default:
            $prog = $val['prog'];
        break;

    }

    $responce->rows[$key]['cell'] = array('', $val['c_id'], $val['location'], $prog );
}

echo json_encode($responce);

?>
